<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="sidebarWidth">
      <div class="logo">
        <img
          src="https://element-plus.org/images/element-plus-logo.svg"
          alt="Logo"
        />
        <span v-show="!store.state.sidebarCollapse">后台管理系统</span>
      </div>
      <el-menu
        :default-active="$route.path"
        router
        :collapse="store.state.sidebarCollapse"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409eff"
        unique-opened
      >
        <el-menu-item index="/dashboard">
          <el-icon><el-icon-menu /></el-icon>
          <template #title>控制台</template>
        </el-menu-item>
        <el-menu-item index="/user">
          <el-icon><el-icon-menu /></el-icon>
          <template #title>用户列表</template>
        </el-menu-item>
        <el-menu-item index="/product">
          <el-icon><el-icon-menu /></el-icon>
          <template #title>商品管理</template>
        </el-menu-item>
        <!-- <el-sub-menu index="2">
          <template #title>
            <el-icon><el-icon-user /></el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/user">用户列表</el-menu-item>
          <el-menu-item index="/user/role">角色管理</el-menu-item>
        </el-sub-menu> -->
        <!-- <el-sub-menu index="3">
          <template #title>
            <el-icon><el-icon-shopping /></el-icon>
            <span>商品管理</span>
          </template>
          <el-menu-item index="/product">商品列表</el-menu-item>
          <el-menu-item index="/product/category">商品分类</el-menu-item>
        </el-sub-menu> -->
      </el-menu>
    </el-aside>

    <el-container>
      <!-- 头部 -->
      <el-header class="layout-header">
        <div class="header-left">
          <el-icon @click="toggleSidebar" class="collapse-icon">
            <component :is="store.state.sidebarCollapse ? 'expand' : 'fold'" />
          </el-icon>
          <Breadcrumb />
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :src="store.state.userInfo.avatar" size="small" />
              <span class="user-name">{{ store.state.userInfo.name }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区域 -->
      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { useStore } from "vuex";
import { useRouter } from "vue-router";
// import Breadcrumb from "../components/Breadcrumb.vue";

// eslint-disable-next-line
/* eslint-disable */
import Breadcrumb from "../components/BaseBreadcrumb.vue";
import {
  Fold,
  Expand,
  Menu as ElIconMenu,
  User as ElIconUser,
  Shopping as ElIconShopping,
} from "@element-plus/icons-vue";

export default {
  components: {
    Breadcrumb,
    Fold,
    Expand,
    ElIconMenu,
    ElIconUser,
    ElIconShopping,
  },
  setup() {
    const store = useStore();
    const router = useRouter();

    const toggleSidebar = () => {
      store.commit("TOGGLE_SIDEBAR");
    };

    const logout = () => {
      store.dispatch("logout");
      router.push("/login");
    };

    return {
      store,
      toggleSidebar,
      logout,
    };
  },
  computed: {
    sidebarWidth() {
      return this.store.state.sidebarCollapse ? "64px" : "200px";
    },
  },
};
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  overflow: hidden;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}

.layout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-icon {
  font-size: 20px;
  cursor: pointer;
  margin-right: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-name {
  margin-left: 8px;
}

.layout-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>
